package com.zhr.tea.view.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.FenLeiContent;

import java.util.List;

/**
 * Created by zhang on 2016/11/1.
 */

public class FenleiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    @Override
    public void onClick(View view) {
        if (recyClickListener != null) {
            recyClickListener.onItemClick((String) view.getTag());

        }
    }

    private RecyClickListener recyClickListener = null;

    public interface RecyClickListener {
        void onItemClick(String title);
    }


    public void setOnItemClickListener(RecyClickListener r) {
        this.recyClickListener = r;
    }


    private Context context;
    private List<FenLeiContent.ContentBean.SubClassListBean> subClassListBeen;

    public FenleiAdapter(Context context, List<FenLeiContent.ContentBean.SubClassListBean> subClassListBeen) {
        this.context = context;
        this.subClassListBeen = subClassListBeen;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fenlei_item, null);
        ItemHoldView vh = new ItemHoldView(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemHoldView && subClassListBeen.size() != 0) {
            ((ItemHoldView) holder).tv.setText(subClassListBeen.get(position).getSubClassName());
            Glide.with(context).load(subClassListBeen.get(position).getSubClassImg()).into(((ItemHoldView) holder).iv);
            holder.itemView.setTag(subClassListBeen.get(position).getSubClassName());
        }
    }

    @Override
    public int getItemCount() {
        return subClassListBeen.size();
    }


    public static class ItemHoldView extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView iv;

        public ItemHoldView(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.fenlei_item_tv);
            iv = (ImageView) itemView.findViewById(R.id.fenlei_item_im);
        }
    }
}
